package com.designpattern.strategy;

import lombok.Data;

/**
 * @author Liusr
 * @date 2023/2/27
 * @package com.designpattern.strategy
 * @Description: 策略模式 + 工厂模式
 */
public class Strategy {

	/**
	 * 抽象策略类
	 */
	abstract static class CashSuper {
		public abstract double acceptCash(double money);
	}

	/**
	 * 正常售价
	 */
	static class CashNormal extends CashSuper {

		@Override
		public double acceptCash(double money) {
			return money;
		}
	}


	/**
	 * 打折
	 */
	@Data
	static
	class CashRebate extends CashSuper {
		//折扣率
		private double rate;

		public CashRebate(double rate) {
			this.rate = rate;
		}

		@Override
		public double acceptCash(double money) {
			return money * rate;
		}
	}

	/**
	 * 返现
	 */
	@Data
	static class CashReturn extends CashSuper {
		//满减条件
		private double moneyCondition = 0.0d;
		//返利金额
		private double moneyReturn = 0.0d;

		public CashReturn(double moneyCondition, double moneyReturn) {
			this.moneyCondition = moneyCondition;
			this.moneyReturn = moneyReturn;
		}

		@Override
		public double acceptCash(double money) {
			double result = money;
			if (money >= moneyCondition) {
				result = money - moneyCondition;
			}
			return result;
		}
	}

	/**
	 * Context,用一个ConcreteStrategy来配置，维护一个对Strategy对象的引用
	 */
	static class CashContext {
		private CashSuper cs = null;

		//初始化时，传入具体策略对象
		public CashContext(String type) {
			switch (type) {
				case "正常收费":
					cs = new CashNormal();
					break;
				case "满300返100":
					cs = new CashReturn(300, 100);
					break;
				case "打8折":
					cs = new CashRebate(0.8);
					break;
			}
		}

		//根据具体的策略对象调用其算法的方法
		public double getResult(double money) {
			return cs.acceptCash(money);
		}
	}

	public static void main(String[] args) {
		CashContext context = new CashContext("打8折");
		System.out.println(context.getResult(1000));
	}
}
